package com.tmc.web.controller.intair;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.tmc.bean.common.ContextInfo;
import com.tmc.bean.common.Pagination;
import com.tmc.bean.common.Report;
import com.tmc.entity.common.EthUser;
import com.tmc.entity.intair.IntairFare;
import com.tmc.entity.intair.IntairOrder;
import com.tmc.service.baseinfo.EthUserServiceImpl;
import com.tmc.service.intair.IntairFairServiceImpl;
import com.tmc.util.ObjectUtil;
import com.tmc.web.BaseController;


@Controller
@RequestMapping(value="/orderreport")
public class OrderReportController extends BaseController{
	
	@Autowired
	private IntairFairServiceImpl faceService;
	
	@Autowired
	private EthUserServiceImpl userService;
	

	/**
	 * 按明细查询返回报表
	 * @param fare
	 * @param order
	 * @param pageIndex
	 * @param pageSize
	 * @return
	 */
	@RequestMapping(value="/list",method=RequestMethod.GET)
	public ModelAndView list(IntairFare fare, IntairOrder order, Integer pageIndex, Integer pageSize, HttpServletRequest request){
		ContextInfo context = this.getContextInfo(request);
		if(!ObjectUtil.hasText(fare.getCondition())){
			fare.setCondition("list");
		}
		if(!ObjectUtil.hasText(fare.getDateCondition())){
			fare.setDateCondition("operatoringTime");
		}
		Map<String,Object> map= new HashMap<String,Object>();
		if(pageIndex == null || pageIndex <= 1){
			pageIndex = 1;
		}
		if(pageSize == null || pageIndex < 1 || pageIndex > 1000){
			pageSize = 10;
		}	
		Pagination<IntairFare> page = faceService.findByFareAndOrder(context, fare, order, pageIndex, pageSize);
		map.put("page", page);
		map.put("fare", fare);
		map.put("order",order);
		map.put("context",context);
		return new ModelAndView("views/report/order-report", map);	
	}
	
	/**
	 * 按网店查询返回报表
	 * @param fare
	 * @param order
	 * @param pageIndex
	 * @param pageSize
	 * @return
	 */
	@RequestMapping(value="/wangDian",method=RequestMethod.GET)
	public ModelAndView wangDian(IntairFare fare, IntairOrder order, Integer pageIndex, Integer pageSize, HttpServletRequest request){
		ContextInfo context = this.getContextInfo(request);
		if(!ObjectUtil.hasText(fare.getCondition())){
			fare.setCondition("wangDian");
		}
		if(!ObjectUtil.hasText(fare.getDateCondition())){
			fare.setDateCondition("0");
		}
		if(pageIndex == null || pageIndex <= 1){
			pageIndex = 1;
		}
		if(pageSize == null || pageIndex < 1 || pageIndex > 1000){
			pageSize = 10;
		}
		Map<String,String> groupBymap= new HashMap<String,String>();
		groupBymap.put("eshop_Id", "eshopId");
		Pagination<Report> page = faceService.wangdian(context, fare, order, pageIndex, pageSize, groupBymap);
		Map<String,Object> map = new HashMap<String,Object>();
		Report report = faceService.sumReport(page.getItems());
		map.put("page", page);
		map.put("report",report);
		map.put("fare", fare);
		map.put("order",order);
		map.put("context",context);
		return new ModelAndView("views/report/order-report", map);	
	}
	
	/**
	 * 按航司查询返回报表
	 * @param fare
	 * @param order
	 * @param pageIndex
	 * @param pageSize
	 * @return
	 */
	@RequestMapping(value="/airline", method=RequestMethod.GET)
	public ModelAndView airline(IntairFare fare, IntairOrder order, Integer pageIndex, Integer pageSize, HttpServletRequest request){
		ContextInfo context = this.getContextInfo(request);
		if(pageIndex == null || pageIndex <= 1){
			pageIndex = 1;
		}
		if(pageSize == null || pageIndex < 1 || pageIndex > 1000){
			pageSize = 10;
		}
		Map<String,String> groupBymap= new HashMap<String,String>();
		groupBymap.put("airline", "airline");
		
		Map<String,Object> map= new HashMap<String,Object>();
		Pagination<Report> page = faceService.findByFareAndOrder(context, fare, order, pageIndex, pageSize, groupBymap);
		Report report = faceService.sumReport(page.getItems());
		map.put("page", page);
		map.put("report",report);
		map.put("fare", fare);
		map.put("order",order);
		map.put("context",context);
		return new ModelAndView("views/report/order-report", map);
		
	}
	
	/**
	 * 按航程查询返回报表
	 * @param fare
	 * @param order
	 * @param pageIndex
	 * @param pageSize
	 * @return
	 */
	@RequestMapping(value="/flight", method=RequestMethod.GET)
	public ModelAndView flight(IntairFare fare, IntairOrder order, Integer pageIndex, Integer pageSize, HttpServletRequest request){
		ContextInfo context = this.getContextInfo(request);
		if(pageIndex == null || pageIndex <= 1){
			pageIndex = 1;
		}
		if(pageSize == null || pageIndex < 1 || pageIndex > 1000){
			pageSize = 10;
		}
		Map<String,String> groupBymap= new HashMap<String,String>();
		groupBymap.put("departure_City", "departureCity");
		groupBymap.put("arrival_City", "arrivalCity");
		
		Map<String,Object> map= new HashMap<String,Object>();
		Pagination<Report> page = faceService.findByFareAndOrder(context, fare, order, pageIndex, pageSize, groupBymap);
		Report report = faceService.sumReport(page.getItems());
		map.put("page", page);
		map.put("report",report);
		map.put("fare", fare);
		map.put("order",order);
		map.put("context",context);
		return new ModelAndView("views/report/order-report", map);
		
	}
	
	/**
	 * 
	 * @param fare
	 * @param order
	 * @param pageIndex
	 * @param pageSize
	 * @return
	 */
	@RequestMapping(value="/airlineAndFlight", method=RequestMethod.GET)
	public ModelAndView airlineAndFlight(IntairFare fare, IntairOrder order, Integer pageIndex, Integer pageSize, HttpServletRequest request){
		ContextInfo context = this.getContextInfo(request);
		if(pageIndex == null || pageIndex <= 1){
			pageIndex = 1;
		}
		if(pageSize == null || pageIndex < 1 || pageIndex > 1000){
			pageSize = 10;
		}
		Map<String,String> groupBymap= new HashMap<String,String>();
		groupBymap.put("airline", "airline");
		groupBymap.put("departure_City", "departureCity");
		groupBymap.put("arrival_City", "arrivalCity");
		
		Map<String,Object> map= new HashMap<String,Object>();
		Pagination<Report> page = faceService.findByFareAndOrder(context, fare, order, pageIndex, pageSize, groupBymap);
		Report report = faceService.sumReport(page.getItems());
		map.put("page", page);
		map.put("report",report);
		map.put("fare", fare);
		map.put("order",order);
		map.put("context",context);
		return new ModelAndView("views/report/order-report", map);
		
	}
	
	@RequestMapping(value="/departureDate", method=RequestMethod.GET)
	public ModelAndView departureDate(IntairFare fare, IntairOrder order, Integer pageIndex, Integer pageSize, HttpServletRequest request){
		ContextInfo context = this.getContextInfo(request);
		if(pageIndex == null || pageIndex <= 1){
			pageIndex = 1;
		}
		if(pageSize == null || pageIndex < 1 || pageIndex > 1000){
			pageSize = 10;
		}
		fare.setDateCondition("departureDate");
		//Map<String,String> groupBymap= new HashMap<String,String>();
		//groupBymap.put("departure_Date", "departureDate");
		Map<String,Object> map= new HashMap<String,Object>();
		Pagination<Report> page = new Pagination<>(pageIndex, pageSize);
		Report report = faceService.sumReport(page.getItems());
		map.put("page", page);
		map.put("report",report);
		map.put("fare", fare);
		map.put("order",order);
		map.put("context",context);
		return new ModelAndView("views/report/order-report", map);
		
	}
	
	@RequestMapping(value="/month", method=RequestMethod.GET)
	public ModelAndView month(IntairFare fare, IntairOrder order, Integer pageIndex, Integer pageSize, HttpServletRequest request){
		ContextInfo context = this.getContextInfo(request);
		if(pageIndex == null || pageIndex <= 1){
			pageIndex = 1;
		}
		if(pageSize == null || pageIndex < 1 || pageIndex > 1000){
			pageSize = 10;
		}
		Map<String,String> groupBymap= new HashMap<String,String>();
		if(ObjectUtil.noText(fare.getDateCondition())){
			fare.setDateCondition("operatoringTime");
		}
		if(fare.getDateCondition().equals("departureDate")){
			groupBymap.put("departure_Date", "departureDate");
		}else{
			groupBymap.put("operatoring_Time", "operatoringTime");
		}
		
		Map<String,Object> map= new HashMap<String,Object>();
		Pagination<Report> page = faceService.findByFareAndOrder(context, fare, order, pageIndex, pageSize, groupBymap);
		Report report = faceService.sumReport(page.getItems());
		map.put("page", page);
		map.put("report",report);
		map.put("fare", fare);
		map.put("order",order);
		map.put("context",context);
		return new ModelAndView("views/report/order-report", map);
		
	}
	
	@RequestMapping(value="/bookOperator", method=RequestMethod.GET)
	public ModelAndView bookOperator(IntairFare fare, IntairOrder order, Integer pageIndex, Integer pageSize, HttpServletRequest request){
		ContextInfo context = this.getContextInfo(request);
		if(pageIndex == null || pageIndex <= 1){
			pageIndex = 1;
		}
		if(pageSize == null || pageIndex < 1 || pageIndex > 1000){
			pageSize = 10;
		}
		Map<String,String> groupBymap= new HashMap<String,String>();
		groupBymap.put("book_Operator_Id", "bookOperatorId");
		
		Map<String,Object> map= new HashMap<String,Object>();
		Pagination<Report> page = faceService.findByFareAndOrder(context, fare, order, pageIndex, pageSize, groupBymap);
		Report report = faceService.sumReport(page.getItems());
		map.put("page", page);
		map.put("report",report);
		map.put("fare", fare);
		map.put("order",order);
		map.put("context",context);
		return new ModelAndView("views/report/order-report", map);
		
	}
	
	@RequestMapping(value="/drawOperator", method=RequestMethod.GET)
	public ModelAndView drawOperator(IntairFare fare, IntairOrder order, Integer pageIndex, Integer pageSize, HttpServletRequest request){
		ContextInfo context = this.getContextInfo(request);
		if(pageIndex == null || pageIndex <= 1){
			pageIndex = 1;
		}
		if(pageSize == null || pageIndex < 1 || pageIndex > 1000){
			pageSize = 10;
		}
		Map<String,String> groupBymap= new HashMap<String,String>();
		groupBymap.put("draw_Operator_Id", "drawOperatorId");
		
		Map<String,Object> map= new HashMap<String,Object>();
		Pagination<Report> page = faceService.findByFareAndOrder(context, fare, order, pageIndex, pageSize, groupBymap);
		Report report = faceService.sumReport(page.getItems());
		map.put("page", page);
		map.put("report",report);
		map.put("fare", fare);
		map.put("order",order);
		map.put("context",context);
		return new ModelAndView("views/report/order-report", map);
		
	}
	
	@RequestMapping(value="/searchOperator", method=RequestMethod.POST)
	@ResponseBody
	public String searchOperator(String query){
		List<EthUser> list=null;
		list = userService.searchOperator(query);
		JSONArray array = new JSONArray();
		for(EthUser user : list){
			//"Key":"clientId","Name":"userName" 注意，键和值都是字符串
			JSONObject obj = new JSONObject();
			obj.put("Key", user.getUserId() 
					+ "");
			obj.put("Name",user.getUserName()+"("+user.getUserCode()+")");
			array.add(obj);
		}
 		return array.toJSONString();	
	}
	
	
}